package com.framework.modules.sys.controller;

import com.framework.entity.ter.TerGoods;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.page.PageUtils;
import com.framework.service.GoodsBannerService;
import com.framework.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 商品相关
 * @author wq
 */
@RestController
@RequestMapping("/sys/goods")
public class TerGoodsController extends AbstractController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsBannerService goodsBannerService;

    /**
     * 商品列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public RespResult querygooodsList(@RequestParam Map<String, Object> params) {
        params.put("deptId",getDeptId());
        params.put("isShow", null);
        PageUtils page = goodsService.queryGoodsInfoList(params);
        return RespResult.ok().put("page", page);
    }

    /**
     * 添加商品
     *
     * @param coverImg
     * @param infoImg
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/addGoods")
    public RespResult addNewGoods(@RequestParam("coverImg") MultipartFile coverImg, @RequestParam("infoImg") MultipartFile infoImg, HttpServletRequest request) throws IOException {
        String prefix;
        Long depId=getDeptId();
        switch (depId.intValue()){
            case 1:
                prefix = "goods";
                break;
            case 889323:
                prefix = "kayi";
                break;
            case 889324:
                prefix = "xinchuang";
                break;
            default:
                prefix = "goods";
                break;
        }

        //上传封面图
        String coverSuffix = coverImg.getOriginalFilename().substring(coverImg.getOriginalFilename().lastIndexOf("."));
        String coverUrl = OSSFactory.build().uploadSuffix(coverImg.getBytes(), coverSuffix, prefix);
        //上传详情图
        String infoSuffix = infoImg.getOriginalFilename().substring(infoImg.getOriginalFilename().lastIndexOf("."));
        String infoUrl = OSSFactory.build().uploadSuffix(infoImg.getBytes(), infoSuffix, prefix);
        Integer count = goodsService.addnNewGoods(coverUrl, infoUrl, request,depId);
        return RespResult.ok().put("count", count);
    }

    /**
     * 添加轮播图
     *
     * @param file
     * @param goodsId
     * @return
     */
    @RequestMapping("/addBanner")
    public RespResult addBanner(@RequestParam("file") MultipartFile[] file, @RequestParam("goodsId") String goodsId) throws IOException {
        String prefix;
        Long depId=getDeptId();
        switch (depId.intValue()){
            case 1:
                prefix = "banner";
                break;
            case 889323:
                prefix = "kayi";
                break;
            case 889324:
                prefix = "xinchuang";
                break;
            default:
                prefix = "banner";
                break;
        }
        String[] bannerUrls = new String[file.length];
        String suffix;
        for (int i = 0; i < file.length; i++) {
            //上传轮播图图
            suffix = file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf("."));
            bannerUrls[i] = OSSFactory.build().uploadSuffix(file[i].getBytes(), suffix, prefix);

        }
        goodsBannerService.insertBannersList(bannerUrls, goodsId);

        return RespResult.ok();
    }

    /**
     * 通过id查找商品
     *
     * @param goodsId
     * @return
     */
    @RequestMapping("/findById/{goodsId}")
    public RespResult findById(@PathVariable Long goodsId) {
        TerGoods goods = goodsService.queryGoodsByGoodsId(goodsId);
        return RespResult.ok().put("goods", goods);
    }

    /**
     * 修改商品
     *
     * @param coverImg
     * @param infoImg
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/updateGoods")
    public RespResult updateGoods(@RequestParam("coverImg") MultipartFile coverImg, @RequestParam("infoImg") MultipartFile infoImg, HttpServletRequest request) throws IOException {
        String prefix;
        Long depId=getDeptId();
        switch (depId.intValue()){
            case 1:
                prefix = "goods";
                break;
            case 889323:
                prefix = "kayi";
                break;
            case 889324:
                prefix = "xinchuang";
                break;
            default:
                prefix = "goods";
                break;
        }

        String coverUrl = "";
        String infoUrl = "";

        if (coverImg.getSize()>0) {
            //上传封面图
            String coverSuffix = coverImg.getOriginalFilename().substring(coverImg.getOriginalFilename().lastIndexOf("."));
            coverUrl = OSSFactory.build().uploadSuffix(coverImg.getBytes(), coverSuffix, prefix);
        }
        if (infoImg.getSize()>0) {
            //上传详情图
            String infoSuffix = infoImg.getOriginalFilename().substring(infoImg.getOriginalFilename().lastIndexOf("."));
            infoUrl = OSSFactory.build().uploadSuffix(infoImg.getBytes(), infoSuffix, prefix);
        }
        Integer count = goodsService.updateGoods(coverUrl, infoUrl, request);
        return RespResult.ok().put("count", count);
    }

    @RequestMapping("/sellGood/{goodsId}")
    public RespResult sellGood(@PathVariable Long goodsId) {
        goodsService.sellGood(goodsId);
        return RespResult.ok();
    }

    @RequestMapping("/unsellGoods/{goodsId}")
    public RespResult unSellGood(@PathVariable Long goodsId) {
        goodsService.unSellGood(goodsId);
        return RespResult.ok();
    }

    @RequestMapping("/unSoldlist")
    public RespResult unSoldlist(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        PageUtils page = goodsService.queryUnSoldList(params);
        return RespResult.ok().put("page", page);
    }

}
